/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.childcare.until;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author BaoTien
 */
public class ConfigDatabase {

    /**
     *
     * @return
     * @throws IOException
     */
    Connection cn = null;
    private String url = "";
    private String serverName = "";
    private String portNumber = "";
    private String databaseName = "";
    private String userName = "";
    private String passWord = "";
    private String lastError;

    public static Connection getConnection() throws IOException {
        try {
            //creating ọne stream to read data.
            FileInputStream fis = null;
            String path = System.getProperty("user.dir") + "/configServer.properties";
            //open Stream, if a path is not found, Exception will be made.
            fis = new FileInputStream(path);
            Properties prop = new Properties();
            prop.load(fis);
            String server = prop.getProperty("server");
            String port = prop.getProperty("port");
            String database = prop.getProperty("database");
            String password = prop.getProperty("password");
            String username = prop.getProperty("username");
            String url = "jdbc:sqlserver://" + server + ":" + port + ";DatabaseName=" + database;
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection cn = java.sql.DriverManager.getConnection(url, username, password);
            fis.close();
            return cn;
        } catch (SQLException ex) {
            Logger.getLogger(ConfigDatabase.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConfigDatabase.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * Close connection
     */
        public static void closeConnection(Object obj){
        try{
            if (obj instanceof Connection)
            {
                ((Connection)obj).close();
            }
            if (obj instanceof PreparedStatement)
            {
                ((PreparedStatement)obj).close();
            }

            if(obj instanceof ResultSet){
                ((ResultSet)obj).close();
            }
        }
        catch(SQLException se)
        {
            JOptionPane.showMessageDialog(new JFrame(),"Connect error","Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the serverName
     */
    public String getServerName() {
        return serverName;
    }

    /**
     * @param serverName the serverName to set
     */
    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    /**
     * @return the portNumber
     */
    public String getPortNumber() {
        return portNumber;
    }

    /**
     * @param portNumber the portNumber to set
     */
    public void setPortNumber(String portNumber) {
        this.portNumber = portNumber;
    }

    /**
     * @return the databaseName
     */
    public String getDatabaseName() {
        return databaseName;
    }

    /**
     * @param databaseName the databaseName to set
     */
    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the passWord
     */
    public String getPassWord() {
        return passWord;
    }

    /**
     * @param passWord the passWord to set
     */
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
}